<template>
  <el-dialog :title="'引用被测评对象'" :close-on-click-modal="false" :visible.sync="visible"
             width="800px" top="25vh">
    <el-container>
      <el-main style="height:100px;">
        <el-form ref="form" :model="form" label-width="120px">
          <el-row>
            <el-col>
              <el-form-item label="引用关系" prop="selectId"
                            :rules="[{ required: true, message: '请选择引用关系', trigger: 'blur' }]">
                <el-select v-model="form.selectId" filterable placeholder="请选择引用关系">
                  <template v-for="(t,i) in dataList">
                    <el-option :label="t.gzName" :value="t.id" v-if="t.id!=gxId&&t.gzBpfdx==gzBpfdx"></el-option>
                  </template>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <span style="margin-left: 60px;">注：选择引用的关系并确定后，系统会将选中关系的被测评对象引用复制给当前配置。</span>
            </el-col>
          </el-row>
        </el-form>
      </el-main>
    </el-container>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="canSubmit" @click="save">确定</el-button>
      <el-button type="primary" plain @click="btnCancel">取消</el-button>
    </span>

  </el-dialog>
</template>

<script>
import {fetchList} from '@/api/ceping/cepingcpgzgzxxb'
import {
  addListObj,
  getBcpmxb
} from '@/api/ceping/cepingcpgzbcpmxb'

export default {
  name: "gzyyForm.vue",
  data() {
    return {
      visible: false,
      gxId: null,
      form: {
        selectId: null
      },
      gzBpfdx: "",
      dataList: [],
      canSubmit: true
    }
  },
  methods: {
    init(id, gzBpfdx) {
      this.canSubmit = true;
      this.form.selectId = null;
      this.gxId = id;
      this.gzBpfdx = gzBpfdx;
      fetchList(Object.assign({
        current: 1,
        size: 500,
      }, this.dataForm)).then(response => {
        this.dataList = response.data.data.records;
       //console.log(this.dataList)
        this.visible = true;
      });
    },
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          //查询被测评对象列表
          getBcpmxb(this.form.selectId).then(response => {
            var obj = response.data.data;
           //console.log(obj);
            if(obj.length==0){
              this.canSubmit = true;
              this.$notify.closeAll();
              this.$notify.error("选择的关系中没有被测评对象");
              return;
            }
            let tableDataUser = [];
            for(let i in obj){
              tableDataUser.push({
                id: null,
                userId: obj[i].userId,
                userName: obj[i].userName,
                deptName: obj[i].deptName,
                deptId: obj[i].deptId,
                gzId: this.gxId,
                bcpPx: obj[i].bcpPx,
              });
            }
            addListObj(tableDataUser).then(data => {
              this.$notify.closeAll();
              this.$notify.success('引用成功');
              this.visible = false;
              this.$emit('showRemarkList');
            }).catch(() => {
              this.canSubmit = true;
            });
          }).catch(() => {
            this.canSubmit = true;
            this.$notify.closeAll();
            this.$notify.error("加载被测评人员失败");
          });

        }
      });
    },
    //取消按钮
    btnCancel() {
      this.visible = false;
    }
  }
}
</script>

<style scoped>

</style>
